package handler

import (
	"NewsHub/model"
	"github.com/gin-gonic/gin"
	"strconv"
)

func ArticleList(c *gin.Context) {
	var article *[]model.Article
	if err := model.DB.Find(&article).Error; err != nil {
		c.JSON(200, gin.H{
			"msg":  "列表展示失败",
			"code": 400,
		})
		return
	}
	c.JSON(200, gin.H{
		"msg":  "列表展示成功",
		"code": 200,
		"list": article,
	})
}
func Like(c *gin.Context) {
	id, _ := strconv.Atoi(c.PostForm("article_id"))
	var favorite model.Favorite
	var article model.Article
	favorite.ID = uint(id)
	if err := model.DB.Model(&article).Update("article.like_count = like_count+?", 1).Error; err != nil {
		c.JSON(200, gin.H{
			"msg":  "点赞失败",
			"code": 400,
		})
		return
	}
	if err := model.DB.Create(&favorite).Error; err != nil {
		c.JSON(200, gin.H{
			"msg":  "点赞失败",
			"code": 400,
		})
		return
	}
	c.JSON(200, gin.H{
		"msg":  "点赞成功",
		"code": 200,
	})
	return
}
